package com.shell.arouter.demo.arouter;

import android.content.Context;
import android.content.DialogInterface;

import com.alibaba.android.arouter.facade.Postcard;
import com.alibaba.android.arouter.facade.annotation.Interceptor;
import com.alibaba.android.arouter.facade.callback.InterceptorCallback;
import com.alibaba.android.arouter.facade.template.IInterceptor;
import com.durian.BaseApp;
import com.durian.base.frame.easyrouter.EasyRouter;
import com.durian.router.LoginRouter;
import com.durian.router.UserRouter;
import com.durian.ui.dialog.DialogAlert;

/**
 * 说明：LoginInteceptor
 * <p/>
 * 作者：fanly
 * <p/>
 * 类型：Class
 * <p/>
 * 时间：2019-06-21 11:33
 * <p/>
 * 版本：version 1.0
 */
@Interceptor(priority = XRouter.Interceptor.login)
public class LoginInteceptor implements IInterceptor {

    @Override
    public void process(Postcard postcard, InterceptorCallback callback) {
        int extra = EasyRouter.getExtra(postcard);
        handleLoginExtra(extra,postcard,callback);
    }

    private void handleLoginExtra(int extra,Postcard postcard, InterceptorCallback callback){
        if (extra == XRouter.Extra.NEED_LOGIN){
            IUserService service = UserRouter.getUserService();
            if (service.isLogin()){
                callback.onContinue(postcard);
            }else {
                BaseApp.runInMainThread(new Runnable() {
                    @Override
                    public void run() {
                        showDialog(postcard,callback);
                    }
                });
            }
        }else {
            callback.onContinue(postcard);
        }
    }

    private void showDialog(Postcard postcard, InterceptorCallback callback){
        toLogin(postcard,callback);
    }

    private void toLogin(Postcard postcard, InterceptorCallback callback){
//        OnActivityResultListener resultListener = new OnActivityResultListener() {
//            @Override
//            public void onReceiveResult(ActivityResult result) {
//                if (result.isSuccess()){
//                    callback.onContinue(postcard);
//                }else {
//                    callback.onInterrupt(null);
//                }
//            }
//        };
        LoginRouter.pageLogin().navigation();
    }

    @Override
    public void init(Context context) {
    }
}
